package com.lbd.dynamicCode.service;

import com.alibaba.fastjson.JSONObject;
import com.lbd.dynamicCode.bean.MsgRedirection;
import com.lbd.dynamicCode.bean.MsgSendRecord;
import com.lbd.dynamicCode.bean.MsgUser;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.concurrent.CompletableFuture;

/**
 * Demo class
 *
 * @author ${user}
 * @date ${date}
 */
@Service
public class RestService {
    private static final Logger logger = LoggerFactory.getLogger(RestService.class);
    private final String companyUrl = "http://zzl.com/callBack";
    private final String sinaLinkUrl = "http://api.t.sina.com.cn/short_url/shorten.json";

    @Resource
    private DynamicCodeService dynamicCodeService;
    @Resource
    private MsgRedirectionService msgRedirectionService;

    private final RestTemplate restTemplate;

    public RestService(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }

    @Async
    public CompletableFuture<MsgUser> findUser(String user) throws InterruptedException {
        logger.info("Looking up " + user);
        String url = String.format("https://api.github.com/users/%s", user);
        MsgUser results = restTemplate.getForObject(url, MsgUser.class);
        return CompletableFuture.completedFuture(results);
    }

    @Async
    public CompletableFuture<Integer> getShortUrl(MsgSendRecord record) throws InterruptedException, UnsupportedEncodingException {
        logger.info("Looking up " + record);
        String originUrl = String.format(companyUrl + "?id=%s", record.getId());
        String encode = URLEncoder.encode("&phone=" + record.getPhone(), "utf-8");
        String longUrl = String.format(originUrl + "%s", encode);
        //格式冲突，长链接需要编码
        String url = String.format(sinaLinkUrl + "?source=%s&url_long=%s", "2193462718", longUrl);
        logger.info("调用之前->" + url);
        //不想返回数组所以用字符串接受
        String results = restTemplate.getForObject(url, String.class);
        logger.info("返回URL->" + results);
        //去掉json数组标识[]
        String remove = StringUtils.remove(StringUtils.remove(results, "["), "]");
        MsgRedirection msgRedirection = JSONObject.parseObject(remove, MsgRedirection.class);
        String url_long = msgRedirection.getUrl_long();
        int result;
        if (url_long.equals(longUrl)) {
            msgRedirection.setUrl_long(URLDecoder.decode(msgRedirection.getUrl_long(), "utf-8"));
            msgRedirectionService.save(msgRedirection);

            record.setShortLink(msgRedirection.getUrl_short());
            record.setLandUrl(msgRedirection.getUrl_long());
            result = dynamicCodeService.updateInfo(record);
        } else {
            result = 0;
        }
        return CompletableFuture.completedFuture(result);
    }
}
